<script setup lang="ts">
import { WELCOME_DATA } from "@/stores/welcome";
import { onBeforeRouteUpdate, useRouter } from "vue-router";
import { onMounted, onUnmounted, ref } from "vue";
import Lottie from "lottie-web";

const router = useRouter();
let pageId = Number(router.currentRoute.value.params.id) || 1;
const data = ref(WELCOME_DATA[pageId - 1]);
const imgRef = ref<HTMLDivElement>();

onBeforeRouteUpdate(to => {
  Lottie.destroy();
  pageId = Number(to.params.id) || 1;
  data.value = WELCOME_DATA[pageId - 1];
  startAnimation();
});

const startAnimation = () => {
  Lottie.loadAnimation({
    container: imgRef.value!,
    renderer: "svg",
    loop: true,
    autoplay: true,
    animationData: data.value.json
  });
};

onMounted(() => {
  startAnimation();
});

onUnmounted(() => {
  Lottie.destroy();
});

const handleNextPage = () => {
  if (pageId < WELCOME_DATA.length) {
    pageId++;
    router.push({ name: "welcome", params: { id: pageId } });
  } else {
    router.push({ name: "start" });
  }
};

const handleSkip = () => {
  router.push({ name: "start" });
};

</script>

<template>
  <div class="welcome">
    <div ref="imgRef" class="img" />
    <h2>
      <span v-for="text of data.title" :key="text">{{ text }}</span>
    </h2>

    <p>{{ data.desc }}</p>
  </div>

  <footer>
    <button type="button" class="next-btn" @click="handleNextPage">下一页</button>
    <button class="skip-btn" @click="handleSkip">跳过</button>
  </footer>

</template>

<style lang="scss" scoped>
.welcome {
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  text-align: center;

  h2 {
    font-size: 22px;
    line-height: 1.2;
    font-weight: bold;
    display: flex;
    flex-direction: column;
    justify-content: center;
  }

  p {
    color: var(--color-black-secondary);
    font-size: 16px;
    font-weight: normal;
    margin-top: 10px;
  }

  .img {
    height: 44vh;
    object-fit: contain;
  }
}


footer {
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  text-align: center;

  .next-btn {
    color: #fff;
    width: 250px;
    height: 60px;
    font-size: 22px;
    border-radius: 30px;
    margin: 30px 0;
    padding: 10px 25px;
    font-weight: bold;
    cursor: pointer;
    transition: all 0.3s ease;
    box-shadow: inset 2px 2px 2px 0 rgba(255, 255, 255, .5),
    7px 7px 20px 0 rgba(0, 0, 0, .1),
    4px 4px 5px 0 rgba(0, 0, 0, .1);
    outline: none;
    border: none;
    background: rgb(251, 33, 117);
    background: linear-gradient(to left, rgba(251, 33, 117, 1) 0%, #EF5350 100%);
    overflow: hidden;
    letter-spacing: 10px;
    user-select: none;

    &:hover {
      text-decoration: none;
      color: #fff;
    }

    &:active {
      box-shadow: 4px 4px 6px 0 rgba(255, 255, 255, .3),
      -4px -4px 6px 0 rgba(116, 125, 136, .2),
      inset -4px -4px 6px 0 rgba(255, 255, 255, .2),
      inset 4px 4px 6px 0 rgba(0, 0, 0, .2);
    }

  }

  .skip-btn {
    color: var(--color-black-secondary);
    outline: none;
    border: none;
    background: none;
    cursor: pointer;
    font-size: 20px;
    letter-spacing: 6px;
  }
}
</style>
